package com.fanhao.dllo.justforyou.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.NewsViewPageInforAdapter;
import com.fanhao.dllo.justforyou.entity.NewsViewPageInforPhotos;
import com.fanhao.dllo.justforyou.entity.NewsViewPageInforResult;
import com.fanhao.dllo.justforyou.view.SpacesItemDecoration;
import com.fanhao.dllo.justforyou.volley.StringRequestUtf;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;


public class NewsHolder01InforActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private NewsViewPageInforAdapter newsViewPageInforAdapter;

    private TextView setnameTv,creatorTv,datatimeTv,commentTv;
    private ImageView backIv;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();

    private String id;
    private String postid;
    private Context context;
    private int position;

    public NewsHolder01InforActivity() {
        super();
    }

    public NewsHolder01InforActivity(Context context) {
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_view_page_infor);
        this.id=getIntent().getStringExtra("id");
        initView();
        initData();
    }

    /**
     * 初始化组件
     * **/
    private void initView() {
        // recyclerView初始化并绑定布局管理器
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);

        GridLayoutManager gm = new GridLayoutManager(this,1);
        gm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(gm);

        // 初始化适配器并绑定
        newsViewPageInforAdapter = new NewsViewPageInforAdapter(this);
        recyclerView.setAdapter(newsViewPageInforAdapter);

        SpacesItemDecoration decoration = new SpacesItemDecoration(20);
        recyclerView.addItemDecoration(decoration);

        setnameTv = (TextView)findViewById(R.id.setnameTv);
        creatorTv = (TextView)findViewById(R.id.creatorTv);
        datatimeTv = (TextView)findViewById(R.id.datatimeTv);
        commentTv = (TextView)findViewById(R.id.commentTv);
        backIv = (ImageView)findViewById(R.id.backIv);

        commentTv.setOnClickListener(this);
        backIv.setOnClickListener(this);


    }

    /**
     * 进行数据解析
     * */
    private void initData() {
        String url ="http://c.m.163.com/photo/api/set/0096/"+id+".json";
        StringRequestUtf request = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                       Gson gson = new Gson();
                        NewsViewPageInforResult nir = gson.fromJson(response,
                                NewsViewPageInforResult.class);

                           setnameTv.setText(nir.getSetname());
                           creatorTv.setText(nir.getCreator());
                           datatimeTv.setText(nir.getDatatime());
                        postid = nir.getCommenturl();
                        if (nir != null) {
                            List<NewsViewPageInforPhotos> inforPhotos = nir.getPhotos();

                            if (inforPhotos != null) {
                                newsViewPageInforAdapter.addData(inforPhotos);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        volleySingleton.addRequest(request);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backIv:

                finish();
                break;

            case R.id.commentTv:
                Intent intent = new Intent(this,NewsWebViewActivity.class);

                intent.putExtra("newsWebView", postid);
                startActivity(intent);
                break;

        }

    }
}
